//
//  BMWContentCollectionView.swift
//  BMWOpenShare
//
//  Created by Li Shengbing, (Shengbing.Li@partner.bmw.com) on 2019/6/5.
//  Copyright © 2019年 shanghai. All rights reserved.
//

import UIKit

class BMWContentCollectionView: UICollectionView {

    fileprivate var row: Int = 1
    fileprivate let column: Int = 4
    fileprivate let itemMarginSpace: CGFloat = 0
    fileprivate let collectionViewEdgeInsets: UIEdgeInsets = UIEdgeInsets(top: 19, left: 10, bottom: 0, right: 10)
    
    fileprivate let cellIdentifier = "cellIdentifier"
    static var userInfo: [BMWShareManager.ContentKey: Any]?
    var rowDatas: [BMWShareModel]? = nil {
        didSet {
            guard let _ =  rowDatas else { return }
            self.reloadData()
        }
    }
    var horizontalIndexPath: IndexPath?
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        row = BMWShareViewModel.rows == 1 ? 1 : 2
        let itemW: CGFloat = floor((self.bounds.width - (itemMarginSpace * CGFloat(column - 1)) - collectionViewEdgeInsets.left - collectionViewEdgeInsets.right) / CGFloat(column))
        let itemH: CGFloat = floor((self.bounds.height - (itemMarginSpace * CGFloat(row - 1)) - collectionViewEdgeInsets.top - collectionViewEdgeInsets.bottom) / CGFloat(row))
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemW, height: itemH)
        
        layout.minimumLineSpacing = itemMarginSpace
        layout.minimumInteritemSpacing = itemMarginSpace
        layout.sectionInset = collectionViewEdgeInsets
        
        if BMWShareViewModel.rows == 1 {
            layout.scrollDirection = .horizontal
        }else {
            layout.scrollDirection = .vertical
        }
        self.isPagingEnabled = true
        self.backgroundColor = UIColor.clear
    }
    
    fileprivate func setupUI() {
        self.delegate = self
        self.dataSource = self
        self.register(BMWCollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
        self.showsHorizontalScrollIndicator = false
        self.showsVerticalScrollIndicator = false
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

extension BMWContentCollectionView: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.rowDatas?.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! BMWCollectionViewCell
        cell.bmwShareModel = self.rowDatas?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let model = self.rowDatas?[indexPath.row] else { return }
        let newIndexPath = IndexPath(row: horizontalIndexPath?.row ?? 0, section: indexPath.row)
        var platformName: BMWPlatform = .weixin
        if model.title == BMWPlatform.weixin.localizedStringTitle {
            platformName = .weixin
            BMWShareManager.share(contents: BMWContentCollectionView.userInfo ?? [:], platform: BMWShareManager.Platform.weixin(WXSceneSession))
        }else if model.title == BMWPlatform.pengyouquan.localizedStringTitle {
            platformName = .pengyouquan
            BMWShareManager.share(contents: BMWContentCollectionView.userInfo ?? [:], platform: BMWShareManager.Platform.weixin(WXSceneTimeline))
        }else if model.title == BMWPlatform.qq.localizedStringTitle {
            platformName = .qq
            BMWShareManager.share(contents: BMWContentCollectionView.userInfo ?? [:], platform: BMWShareManager.Platform.qq(ShareQQPlatform.qqAnyWhere, ShareDestTypeQQ))
        }else if model.title == BMWPlatform.weibo.localizedStringTitle {
            platformName = .weibo
            BMWShareManager.share(contents: BMWContentCollectionView.userInfo ?? [:], platform: BMWShareManager.Platform.weibo)
        }else if model.title == BMWPlatform.sms.localizedStringTitle {
            platformName = .sms
            BMWShareManager.share(contents: BMWContentCollectionView.userInfo ?? [:], platform: BMWShareManager.Platform.sms)
        }else if model.title == BMWPlatform.copy.localizedStringTitle {
            platformName = .copy
            BMWShareManager.share(contents: BMWContentCollectionView.userInfo ?? [:], platform: BMWShareManager.Platform.copy)
        }

        NotificationCenter.default.post(name: Notification.Name.init("WillDismissShareView"), object: nil, userInfo: ["indexPath": newIndexPath, "title": platformName])
    }
}
